<?php
/*
* @desc 支付网关控制类
* @author kevin.wang
* @date 2015-01-01 12:00:00
*/
class PaymentController extends Controller
{
	//过滤器
	public function filters()
    {
        return array(
            'UserLogin +index',
        );
    }
	//支付请求接口
	public function actionIndex()
	{
		$request = Yii::app();
		$userInfo = json_decode($request->session['userinfo'], true);
		$orderSn = $request->request->getParam("ordersn");
		$order = new Order;
		$mainOrder = $order->getOrderInfo($orderSn, $userInfo['user_id']);
		if(empty($mainOrder)){
			throw new ExceptionClass("未找到订单");
		}
		$rechargeForm = new RechargeForm;
		$rechargeForm->attributes = array('price'=>$mainOrder['total_amount'], 'user_id'=>$userInfo['user_id'],'order_sn'=>$orderSn);
		$rechargeForm->writeRechargeLog();
		$param = $rechargeForm->getRechargeData();
		//$payType = $request->request->getParam("payType");
		$alipaySubmit = new AlipaySubmit();
		header("Content-type:text/html;charset=utf-8");
		$html_text = $alipaySubmit->buildRequestForm($param,"post");
		echo $html_text;
	}
	//支付宝同步回调
	public function actionReturn(){
		unset($_GET['r']);
		Yii::log("同步回调开始:".json_encode($_GET), CLogger::LEVEL_ERROR, "pay.callback.return");
		$alipayNotify = new AlipayNotify();
		$verify_result = $alipayNotify->verifyReturn();
		//$verify_result = true;
		if(!empty($verify_result)){
			$param = $alipayNotify->getRequestParam();
			Yii::log("同步回调验证成功:".json_encode($param), CLogger::LEVEL_ERROR, "pay.callback.return");
			//商户订单号
			$out_trade_no = $param['out_trade_no'];
			//$out_trade_no = '197043197554cc87a39ab80';
			//支付宝交易号
			$trade_no = $param['trade_no'];
			//$trade_no = '1234567890';
			//交易状态
			$trade_status = $param['trade_status'];
			//$trade_status = 'WAIT_SELLER_SEND_GOODS';
			$alipay_config = Yii::app()->params['alipay'];
			$rechargeForm = new RechargeForm;
			$orderForm = new OrderForm;
			$recharge = new Recharge;
			$rechargeLog = $recharge->getRechargeLog($out_trade_no);
			//已处理订单
			if($rechargeLog['status'] != 0){
				Yii::log("同步已处理订单:".json_encode($rechargeLog), CLogger::LEVEL_ERROR, "pay.callback.return");
				$this->redirect(Yii::app()->createUrl("pay/payresult", array('ordersn' => $rechargeLog['order_sn'])));
				Yii::app()->end();
			}
			if(in_array($trade_status, $alipay_config['return_success_flag'])){
				$result = $rechargeForm->handleRechargeLog(1, $out_trade_no);
				if(!empty($result)){
					$orderForm->processOrder(2, $rechargeLog['order_sn']);
				}
				Yii::log("同步回调成功处理:".json_encode($result), CLogger::LEVEL_ERROR, "pay.callback.return");
			}else{
				$rechargeForm->handleRechargeLog(2, $out_trade_no);
				if(!empty($result)){
					$orderForm->processOrder(3, $rechargeLog['order_sn']);
				}
				Yii::log("异步回调失败处理", CLogger::LEVEL_ERROR, "pay.callback.return");
			}
			$this->redirect(Yii::app()->createUrl("pay/payresult", array('ordersn' => $rechargeLog['order_sn'])));
			Yii::app()->end();
		}
		Yii::log("同步回调验证失败", CLogger::LEVEL_ERROR, "pay.callback.return");
		$this->redirect(Yii::app()->createUrl("user/index"));
		Yii::app()->end();
	}
	//支付宝异步回调
	public function actionNotify(){
		unset($_POST['r']);
		Yii::log("异步回调开始:".json_encode($_POST), CLogger::LEVEL_ERROR, "pay.callback.notify");
		$alipayNotify = new AlipayNotify();
		$verify_result = $alipayNotify->verifyNotify();
		if(!empty($verify_result)){
			$param = $alipayNotify->getRequestParam();
			Yii::log("异步回调验证成功:".json_encode($param), CLogger::LEVEL_ERROR, "pay.callback.notify");
			//商户订单号
			$out_trade_no = $param['out_trade_no'];
			//支付宝交易号
			$trade_no = $param['trade_no'];
			//交易状态
			$trade_status = $param['trade_status'];
			$alipay_config = Yii::app()->params['alipay'];
			$rechargeForm = new RechargeForm;
			$orderForm = new OrderForm;
			$recharge = new Recharge;
			$rechargeLog = $recharge->getRechargeLog($out_trade_no);
			//已处理订单
			if($rechargeLog['status'] != 0){
				Yii::log("异步已处理订单:".json_encode($rechargeLog), CLogger::LEVEL_ERROR, "pay.callback.notify");
				echo "success";
				Yii::app()->end();
			}
			if(in_array($trade_status, $alipay_config['notify_success_flag'])){
				$result = $rechargeForm->handleRechargeLog(1, $out_trade_no);
				if(!empty($result)){
					$orderForm->processOrder(2, $rechargeLog['order_sn']);
				}
				Yii::log("异步回调成功处理:".json_encode($result), CLogger::LEVEL_ERROR, "pay.callback.notify");
			}else{
				$rechargeForm->handleRechargeLog(2, $out_trade_no);
				if(!empty($result)){
					$orderForm->processOrder(3, $rechargeLog['order_sn']);
				}
				Yii::log("异步回调失败处理", CLogger::LEVEL_ERROR, "pay.callback.notify");
			}
			echo "success";
			Yii::app()->end();
		}
		Yii::log("异步回调验证失败", CLogger::LEVEL_ERROR, "pay.notify");
		echo "fail";
		Yii::app()->end();
	}
}
